{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Python绘制树状图"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import json\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Tree"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 读取广告转化数据集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据源：https://www.kaggle.com/loveall/clicks-conversion-tracking/data\n",
    "# 广告转化数据集：Ad Conversions Data\n",
    "df = pd.read_csv(\"./datas/KAG_conversion_data.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ad_id</th>\n",
       "      <th>xyz_campaign_id</th>\n",
       "      <th>fb_campaign_id</th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>interest</th>\n",
       "      <th>Impressions</th>\n",
       "      <th>Clicks</th>\n",
       "      <th>Spent</th>\n",
       "      <th>Total_Conversion</th>\n",
       "      <th>Approved_Conversion</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>708746</td>\n",
       "      <td>916</td>\n",
       "      <td>103916</td>\n",
       "      <td>30-34</td>\n",
       "      <td>M</td>\n",
       "      <td>15</td>\n",
       "      <td>7350</td>\n",
       "      <td>1</td>\n",
       "      <td>1.43</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>708749</td>\n",
       "      <td>916</td>\n",
       "      <td>103917</td>\n",
       "      <td>30-34</td>\n",
       "      <td>M</td>\n",
       "      <td>16</td>\n",
       "      <td>17861</td>\n",
       "      <td>2</td>\n",
       "      <td>1.82</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>708771</td>\n",
       "      <td>916</td>\n",
       "      <td>103920</td>\n",
       "      <td>30-34</td>\n",
       "      <td>M</td>\n",
       "      <td>20</td>\n",
       "      <td>693</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    ad_id  xyz_campaign_id  fb_campaign_id    age gender  interest  \\\n",
       "0  708746              916          103916  30-34      M        15   \n",
       "1  708749              916          103917  30-34      M        16   \n",
       "2  708771              916          103920  30-34      M        20   \n",
       "\n",
       "   Impressions  Clicks  Spent  Total_Conversion  Approved_Conversion  \n",
       "0         7350       1   1.43                 2                    1  \n",
       "1        17861       2   1.82                 2                    0  \n",
       "2          693       0   0.00                 1                    0  "
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 数据处理\n",
    "df[\"total\"] = \"全量\"\n",
    "df[\"xyz_campaign_id\"] = df[\"xyz_campaign_id\"].map(lambda x : f\"c_{x}\")\n",
    "df[\"gender\"] = df[\"gender\"].map(lambda x : f\"g_{x}\")\n",
    "df[\"age\"] = df[\"age\"].map(lambda x : f\"a_{x}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ad_id</th>\n",
       "      <th>xyz_campaign_id</th>\n",
       "      <th>fb_campaign_id</th>\n",
       "      <th>age</th>\n",
       "      <th>gender</th>\n",
       "      <th>interest</th>\n",
       "      <th>Impressions</th>\n",
       "      <th>Clicks</th>\n",
       "      <th>Spent</th>\n",
       "      <th>Total_Conversion</th>\n",
       "      <th>Approved_Conversion</th>\n",
       "      <th>total</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>708746</td>\n",
       "      <td>c_916</td>\n",
       "      <td>103916</td>\n",
       "      <td>a_30-34</td>\n",
       "      <td>g_M</td>\n",
       "      <td>15</td>\n",
       "      <td>7350</td>\n",
       "      <td>1</td>\n",
       "      <td>1.43</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>全量</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>708749</td>\n",
       "      <td>c_916</td>\n",
       "      <td>103917</td>\n",
       "      <td>a_30-34</td>\n",
       "      <td>g_M</td>\n",
       "      <td>16</td>\n",
       "      <td>17861</td>\n",
       "      <td>2</td>\n",
       "      <td>1.82</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>全量</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>708771</td>\n",
       "      <td>c_916</td>\n",
       "      <td>103920</td>\n",
       "      <td>a_30-34</td>\n",
       "      <td>g_M</td>\n",
       "      <td>20</td>\n",
       "      <td>693</td>\n",
       "      <td>0</td>\n",
       "      <td>0.00</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>全量</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    ad_id xyz_campaign_id  fb_campaign_id      age gender  interest  \\\n",
       "0  708746           c_916          103916  a_30-34    g_M        15   \n",
       "1  708749           c_916          103917  a_30-34    g_M        16   \n",
       "2  708771           c_916          103920  a_30-34    g_M        20   \n",
       "\n",
       "   Impressions  Clicks  Spent  Total_Conversion  Approved_Conversion total  \n",
       "0         7350       1   1.43                 2                    1    全量  \n",
       "1        17861       2   1.82                 2                    0    全量  \n",
       "2          693       0   0.00                 1                    0    全量  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. 准备数据：全量 > 活动 > 性别 > 年龄 四级树"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>total</th>\n",
       "      <th>xyz_campaign_id</th>\n",
       "      <th>gender</th>\n",
       "      <th>age</th>\n",
       "      <th>size</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>全量</td>\n",
       "      <td>c_1178</td>\n",
       "      <td>g_F</td>\n",
       "      <td>a_30-34</td>\n",
       "      <td>89</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>全量</td>\n",
       "      <td>c_1178</td>\n",
       "      <td>g_F</td>\n",
       "      <td>a_35-39</td>\n",
       "      <td>57</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>全量</td>\n",
       "      <td>c_1178</td>\n",
       "      <td>g_F</td>\n",
       "      <td>a_40-44</td>\n",
       "      <td>60</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "  total xyz_campaign_id gender      age  size\n",
       "0    全量          c_1178    g_F  a_30-34    89\n",
       "1    全量          c_1178    g_F  a_35-39    57\n",
       "2    全量          c_1178    g_F  a_40-44    60"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据统计\n",
    "df_agg = df.groupby([\"total\", \"xyz_campaign_id\", \"gender\", \"age\"]).size().rename(\"size\").reset_index()\n",
    "df_agg.head(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"name\": \"\\u5168\\u91cf\", \"children\": [{\"name\": \"c_1178\", \"children\": [{\"name\": \"g_F\", \"children\": [{\"name\": \"a_30-34\", \"value\": 89}, {\"name\": \"a_35-39\", \"value\": 57}, {\"name\": \"a_40-44\", \"value\": 60}, {\"name\": \"a_45-49\", \"value\": 70}]}, {\"name\": \"g_M\", \"children\": [{\"name\": \"a_30-34\", \"value\": 112}, {\"name\": \"a_35-39\", \"value\": 90}, {\"name\": \"a_40-44\", \"value\": 69}, {\"name\": \"a_45-49\", \"value\": 78}]}]}, {\"name\": \"c_916\", \"children\": [{\"name\": \"g_F\", \"children\": [{\"name\": \"a_30-34\", \"value\": 11}, {\"name\": \"a_35-39\", \"value\": 3}, {\"name\": \"a_40-44\", \"value\": 1}, {\"name\": \"a_45-49\", \"value\": 4}]}, {\"name\": \"g_M\", \"children\": [{\"name\": \"a_30-34\", \"value\": 18}, {\"name\": \"a_35-39\", \"value\": 9}, {\"name\": \"a_40-44\", \"value\": 5}, {\"name\": \"a_45-49\", \"value\": 3}]}]}, {\"name\": \"c_936\", \"children\": [{\"name\": \"g_F\", \"children\": [{\"name\": \"a_30-34\", \"value\": 97}, {\"name\": \"a_35-39\", \"value\": 49}, {\"name\": \"a_40-44\", \"value\": 46}, {\"name\": \"a_45-49\", \"value\": 64}]}, {\"name\": \"g_M\", \"children\": [{\"name\": \"a_30-34\", \"value\": 99}, {\"name\": \"a_35-39\", \"value\": 40}, {\"name\": \"a_40-44\", \"value\": 29}, {\"name\": \"a_45-49\", \"value\": 40}]}]}]}\n"
     ]
    }
   ],
   "source": [
    "data = {}\n",
    "data[\"name\"] = \"全量\"\n",
    "data[\"children\"] = []\n",
    "\n",
    "for campaign in df_agg[\"xyz_campaign_id\"].unique():\n",
    "    campaign_node = {}\n",
    "    campaign_node[\"name\"] = campaign\n",
    "    campaign_node[\"children\"] = []\n",
    "    \n",
    "    for gender in df_agg[df_agg[\"xyz_campaign_id\"]==campaign][\"gender\"].unique():\n",
    "        gender_node = {}\n",
    "        gender_node[\"name\"] = gender\n",
    "        gender_node[\"children\"] = []\n",
    "        \n",
    "        age_datalist = (\n",
    "            df_agg[\n",
    "                (df_agg[\"xyz_campaign_id\"]==campaign)\n",
    "                & (df_agg[\"gender\"]==gender)\n",
    "            ][[\"age\", \"size\"]]\n",
    "        )\n",
    "        \n",
    "        for age,size in age_datalist.values:\n",
    "            age_node = {}\n",
    "            age_node[\"name\"] = age\n",
    "            # 叶子结点变成了value\n",
    "            age_node[\"value\"] = size\n",
    "            gender_node[\"children\"].append(age_node)\n",
    "        \n",
    "        campaign_node[\"children\"].append(gender_node)\n",
    "    \n",
    "    data[\"children\"].append(campaign_node)\n",
    "\n",
    "print(json.dumps(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<script>\n",
       "    require.config({\n",
       "        paths: {\n",
       "            'echarts':'https://assets.pyecharts.org/assets/echarts.min'\n",
       "        }\n",
       "    });\n",
       "</script>\n",
       "\n",
       "        <div id=\"47551d6a83f8432485577a5eea4de57a\" style=\"width:900px; height:500px;\"></div>\n",
       "\n",
       "<script>\n",
       "        require(['echarts'], function(echarts) {\n",
       "                var chart_47551d6a83f8432485577a5eea4de57a = echarts.init(\n",
       "                    document.getElementById('47551d6a83f8432485577a5eea4de57a'), 'white', {renderer: 'canvas'});\n",
       "                var option_47551d6a83f8432485577a5eea4de57a = {\n",
       "    \"animation\": true,\n",
       "    \"animationThreshold\": 2000,\n",
       "    \"animationDuration\": 1000,\n",
       "    \"animationEasing\": \"cubicOut\",\n",
       "    \"animationDelay\": 0,\n",
       "    \"animationDurationUpdate\": 300,\n",
       "    \"animationEasingUpdate\": \"cubicOut\",\n",
       "    \"animationDelayUpdate\": 0,\n",
       "    \"color\": [\n",
       "        \"#c23531\",\n",
       "        \"#2f4554\",\n",
       "        \"#61a0a8\",\n",
       "        \"#d48265\",\n",
       "        \"#749f83\",\n",
       "        \"#ca8622\",\n",
       "        \"#bda29a\",\n",
       "        \"#6e7074\",\n",
       "        \"#546570\",\n",
       "        \"#c4ccd3\",\n",
       "        \"#f05b72\",\n",
       "        \"#ef5b9c\",\n",
       "        \"#f47920\",\n",
       "        \"#905a3d\",\n",
       "        \"#fab27b\",\n",
       "        \"#2a5caa\",\n",
       "        \"#444693\",\n",
       "        \"#726930\",\n",
       "        \"#b2d235\",\n",
       "        \"#6d8346\",\n",
       "        \"#ac6767\",\n",
       "        \"#1d953f\",\n",
       "        \"#6950a1\",\n",
       "        \"#918597\"\n",
       "    ],\n",
       "    \"series\": [\n",
       "        {\n",
       "            \"type\": \"tree\",\n",
       "            \"data\": [\n",
       "                {\n",
       "                    \"name\": \"\\u5168\\u91cf\",\n",
       "                    \"children\": [\n",
       "                        {\n",
       "                            \"name\": \"c_1178\",\n",
       "                            \"children\": [\n",
       "                                {\n",
       "                                    \"name\": \"g_F\",\n",
       "                                    \"children\": [\n",
       "                                        {\n",
       "                                            \"name\": \"a_30-34\",\n",
       "                                            \"value\": 89\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_35-39\",\n",
       "                                            \"value\": 57\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_40-44\",\n",
       "                                            \"value\": 60\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_45-49\",\n",
       "                                            \"value\": 70\n",
       "                                        }\n",
       "                                    ]\n",
       "                                },\n",
       "                                {\n",
       "                                    \"name\": \"g_M\",\n",
       "                                    \"children\": [\n",
       "                                        {\n",
       "                                            \"name\": \"a_30-34\",\n",
       "                                            \"value\": 112\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_35-39\",\n",
       "                                            \"value\": 90\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_40-44\",\n",
       "                                            \"value\": 69\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_45-49\",\n",
       "                                            \"value\": 78\n",
       "                                        }\n",
       "                                    ]\n",
       "                                }\n",
       "                            ]\n",
       "                        },\n",
       "                        {\n",
       "                            \"name\": \"c_916\",\n",
       "                            \"children\": [\n",
       "                                {\n",
       "                                    \"name\": \"g_F\",\n",
       "                                    \"children\": [\n",
       "                                        {\n",
       "                                            \"name\": \"a_30-34\",\n",
       "                                            \"value\": 11\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_35-39\",\n",
       "                                            \"value\": 3\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_40-44\",\n",
       "                                            \"value\": 1\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_45-49\",\n",
       "                                            \"value\": 4\n",
       "                                        }\n",
       "                                    ]\n",
       "                                },\n",
       "                                {\n",
       "                                    \"name\": \"g_M\",\n",
       "                                    \"children\": [\n",
       "                                        {\n",
       "                                            \"name\": \"a_30-34\",\n",
       "                                            \"value\": 18\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_35-39\",\n",
       "                                            \"value\": 9\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_40-44\",\n",
       "                                            \"value\": 5\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_45-49\",\n",
       "                                            \"value\": 3\n",
       "                                        }\n",
       "                                    ]\n",
       "                                }\n",
       "                            ]\n",
       "                        },\n",
       "                        {\n",
       "                            \"name\": \"c_936\",\n",
       "                            \"children\": [\n",
       "                                {\n",
       "                                    \"name\": \"g_F\",\n",
       "                                    \"children\": [\n",
       "                                        {\n",
       "                                            \"name\": \"a_30-34\",\n",
       "                                            \"value\": 97\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_35-39\",\n",
       "                                            \"value\": 49\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_40-44\",\n",
       "                                            \"value\": 46\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_45-49\",\n",
       "                                            \"value\": 64\n",
       "                                        }\n",
       "                                    ]\n",
       "                                },\n",
       "                                {\n",
       "                                    \"name\": \"g_M\",\n",
       "                                    \"children\": [\n",
       "                                        {\n",
       "                                            \"name\": \"a_30-34\",\n",
       "                                            \"value\": 99\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_35-39\",\n",
       "                                            \"value\": 40\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_40-44\",\n",
       "                                            \"value\": 29\n",
       "                                        },\n",
       "                                        {\n",
       "                                            \"name\": \"a_45-49\",\n",
       "                                            \"value\": 40\n",
       "                                        }\n",
       "                                    ]\n",
       "                                }\n",
       "                            ]\n",
       "                        }\n",
       "                    ]\n",
       "                }\n",
       "            ],\n",
       "            \"symbol\": \"emptyCircle\",\n",
       "            \"symbolSize\": 7,\n",
       "            \"roam\": false,\n",
       "            \"expandAndCollapse\": true,\n",
       "            \"layout\": \"orthogonal\",\n",
       "            \"orient\": \"LR\",\n",
       "            \"label\": {\n",
       "                \"show\": true,\n",
       "                \"position\": \"top\",\n",
       "                \"margin\": 8,\n",
       "                \"align\": \"right\",\n",
       "                \"verticalAlign\": \"middle\"\n",
       "            },\n",
       "            \"leaves\": {\n",
       "                \"label\": {\n",
       "                    \"show\": true,\n",
       "                    \"position\": \"top\",\n",
       "                    \"margin\": 8\n",
       "                }\n",
       "            }\n",
       "        }\n",
       "    ],\n",
       "    \"legend\": [\n",
       "        {\n",
       "            \"data\": [],\n",
       "            \"selected\": {},\n",
       "            \"show\": true,\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10,\n",
       "            \"itemWidth\": 25,\n",
       "            \"itemHeight\": 14\n",
       "        }\n",
       "    ],\n",
       "    \"tooltip\": {\n",
       "        \"show\": true,\n",
       "        \"trigger\": \"item\",\n",
       "        \"triggerOn\": \"mousemove|click\",\n",
       "        \"axisPointer\": {\n",
       "            \"type\": \"line\"\n",
       "        },\n",
       "        \"textStyle\": {\n",
       "            \"fontSize\": 14\n",
       "        },\n",
       "        \"borderWidth\": 0\n",
       "    },\n",
       "    \"title\": [\n",
       "        {\n",
       "            \"text\": \"\\u5e7f\\u544a\\u6d41\\u91cf\\u56fe\",\n",
       "            \"left\": \"center\",\n",
       "            \"padding\": 5,\n",
       "            \"itemGap\": 10\n",
       "        }\n",
       "    ]\n",
       "};\n",
       "                chart_47551d6a83f8432485577a5eea4de57a.setOption(option_47551d6a83f8432485577a5eea4de57a);\n",
       "        });\n",
       "    </script>\n"
      ],
      "text/plain": [
       "<pyecharts.render.display.HTML at 0x1d50ad2bc48>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "tree = (\n",
    "    Tree()\n",
    "    .add(\n",
    "        \"\",\n",
    "        [data],\n",
    "        # 参数可以有：取值分别为 'LR' , 'RL', 'TB', 'BT'\n",
    "        orient=\"LR\",\n",
    "        # layout：orthogonal，radial，如果是radial则是圆环发散图\n",
    "        #layout=\"radial\",\n",
    "        label_opts=opts.LabelOpts(\n",
    "            position=\"top\",\n",
    "            horizontal_align=\"right\",\n",
    "            vertical_align=\"middle\",\n",
    "        ),\n",
    "    )\n",
    "    .set_global_opts(title_opts=opts.TitleOpts(title=\"广告流量图\", pos_left=\"center\"))\n",
    ")\n",
    "tree.render_notebook()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
